home *** CD-ROM | disk | FTP | other *** search
/ Mastering Web Site Development / Microsoft Mastering Web Site Development (Microsoft) (1997).iso / Labs / Lab08 / Solution / enrollment.cls < prev    next >
Encoding:
Visual Basic class definition  |  1997-04-24  |  5.0 KB  |  167 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Enrollment"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Const MAXCLASSES = 6
  11. Function OverEnrolled(conn As ADODB.Connection, lngStudentID As Long) As Boolean
  12.     Dim rsEnrollment As Object
  13.     
  14.     'Student cannot add if already at class limit
  15.     strSQL = "select count(studentid) from enrollment where studentid = " & lngStudentID
  16.     Set rsEnrollment = CreateObject("ADODB.recordset")
  17.     rsEnrollment.ActiveConnection = conn
  18.     rsEnrollment.Open strSQL, , adOpenKeyset
  19.     If rsEnrollment.Fields(0).Value > MAXCLASSES Then
  20.         OverEnrolled = True
  21.     Else
  22.         OverEnrolled = False
  23.     End If
  24.     rsEnrollment.Close
  25. End Function
  26. Function ClassCompleted(conn As ADODB.Connection, lngStudentID As Long, strClassID As String) As Boolean
  27.     Dim rsEnrollment As Object
  28.     
  29.     'Student cannot drop a class if the class is completed
  30.     'In other words, it has a grade
  31.     strSQL = "select classid,studentid,grade from enrollment where studentid = " & lngStudentID & " AND classid ='" & strClassID & "'"
  32.     Set rsEnrollment = conn.Execute(strSQL)
  33.     
  34.     'Check for no records even existing
  35.     If rsEnrollment.BOF And rsEnrollment.EOF Then
  36.        ClassCompleted = False
  37.        Exit Function
  38.     End If
  39.     
  40.     'Check for a non-null grade
  41.     If Not IsNull(rsEnrollment.Fields("Grade")) Then
  42.         ClassCompleted = True
  43.     Else
  44.         ClassCompleted = False
  45.     End If
  46.     rsEnrollment.Close
  47. End Function
  48. Public Function Add(ByVal lngStudentID As Long, ByVal strClassID As String) As Integer
  49.  
  50.     Dim conn As ADODB.Connection
  51.     Dim strSQL As String
  52.     
  53.     'Set return value to 0
  54.     Add = 0
  55.     Set conn = CreateObject("ADODB.Connection")
  56.     On Error GoTo ErrorHandler
  57.     'Open connection with State University Database
  58.     conn.Open "DSN=StateU;UID=sa;PWD=;"
  59.      
  60.     'Business rule
  61.     'See if student is over enrolled
  62.     If OverEnrolled(conn, lngStudentID) Then
  63.         Add = 1
  64.         Exit Function
  65.     End If
  66.     
  67.     'Perform the work of adding the student to the class
  68.     strSQL = "INSERT INTO enrollment (ClassID, StudentID) VALUES ('" & strClassID & "'," & lngStudentID & ")"
  69.     conn.BeginTrans
  70.     conn.Execute strSQL
  71.     conn.CommitTrans
  72.     
  73.     'Close the connection
  74.     conn.Close
  75.     Exit Function
  76.  
  77. ErrorHandler:
  78.     'Rollback the transaction if we got that far
  79.     If Not IsEmpty(conn) Then conn.RollbackTrans
  80.     
  81.     'Raise an error to report back
  82.     Add = 1
  83. End Function
  84.  
  85. Public Function Transfer(ByVal lngStudentID As Long, ByVal strSrcClassID As String, ByVal strDstClassID As String) As Integer
  86.     On Error GoTo ErrorHandler
  87.     Dim conn As ADODB.Connection
  88.     Dim strDropSQL, strAddSQL As String
  89.         
  90.     Transfer = 0
  91.     Set conn = CreateObject("ADODB.connection")
  92.  
  93.     'Create update strings
  94.     strDropSQL = "DELETE FROM enrollment where classid = '" & strSrcClassID & "' and studentid = " & lngStudentID
  95.     strAddSQL = "INSERT INTO enrollment (ClassID, StudentID) VALUES ('" & strDstClassID & "'," & lngStudentID & ")"
  96.  
  97.     'Open connection with State University Database
  98.     conn.Open "DSN=StateU;UID=sa;PWD=;"
  99.  
  100.     'Check that student is not dropping a class that
  101.     'is already completed
  102.     If ClassCompleted(conn, lngStudentID, strSrcClassID) Then
  103.         Transfer = 1
  104.         Exit Function
  105.     End If
  106.  
  107.     'Perform work of transferring student
  108.     conn.BeginTrans
  109.     conn.Execute strDropSQL
  110.     conn.Execute strAddSQL
  111.     conn.CommitTrans
  112.  
  113.     'Close connection
  114.     conn.Close
  115.     Exit Function
  116.  
  117. ErrorHandler:
  118.     'Rollback the transaction if we got that far
  119.     If Not IsEmpty(conn) Then conn.RollbackTrans
  120.     
  121.     'Raise an error to report back
  122.     Transfer = 1
  123.  
  124. End Function
  125.  
  126. Public Function Drop(ByVal lngStudentID As Long, _
  127. ByVal strClassID As String) As Integer
  128.     On Error GoTo ErrorHandler
  129.     Dim conn As ADODB.Connection
  130.     
  131.     Dim strSQL As String
  132.     
  133.     Drop = 0
  134.     Set conn = CreateObject("ADODB.connection")
  135.     'Open connection with State University Database
  136.     conn.Open "DSN=StateU;UID=sa;PWD=;"
  137.      
  138.     'Check business rule
  139.     'Make sure class isn't already completed
  140.     'In which case student cannot drop
  141.     If ClassCompleted(conn, lngStudentID, strClassID) Then
  142.         Drop = 1
  143.         Exit Function
  144.     End If
  145.     
  146.     'Perform the work of dropping the student from the class
  147.     strSQL = "DELETE FROM enrollment where ClassID ='" & strClassID & "' AND Studentid =" & lngStudentID
  148.     conn.BeginTrans
  149.     conn.Execute strSQL
  150.     conn.CommitTrans
  151.     
  152.     'Close the connection
  153.     conn.Close
  154.     Exit Function
  155.  
  156. ErrorHandler:
  157.     'Rollback the transaction if we got that far
  158.     If Not IsEmpty(conn) Then conn.RollbackTrans
  159.     
  160.     'Raise an error to report back
  161.     Drop = 1
  162.     'Check for student not enrolled to begin with 4
  163.  
  164. End Function
  165.  
  166.  
  167.